/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tkgarment.system.action;

import static com.opensymphony.xwork2.Action.ERROR;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.logging.Logger;
import com.tkgarment.system.dao.DepartmentDAO;
import com.tkgarment.system.domain.Department;
import com.tkgarment.system.factory.DAOFactory;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author zraj
 */
public class DepartmentAction extends  ActionSupportBase implements SessionAware{

    Map<String,Object> session;

    private String deptID;
    private Department dept;
    private Department newDept;
    private List<Department> listDept;
    private String searchStr;
    
    
    
    
    public String execute() throws Exception {
        if (session.get("deptlist") != null) {
             session.remove("deptlist");
        }
        return "success";
    }
    
    
    public String deleteDept() throws Exception {
        
        DepartmentDAO dao = DAOFactory.createDepartmentDao();
        
        int i = dao.deleteSingle(dept);
//        setDeptID(dept.getDepartmentID());
        
        if (i == 1) {
            setMessage("ลบข้อมูลเรียบร้อยแล้ว");
           return "success"; 
        }else if(i == 99){
           setMessage("ไม่สามารถลบข้อมูลได้ ข้อมูลนี้มีการใช้งานอยู่ ("+ dept.getDepartmentID()  +":"+ newDept.getDepartmentName() +")");
           return SUCCESS; 
        }else{
            return ERROR;
        }
        
      
    }
    
    
    public String formAddDept()throws Exception{
        return SUCCESS;
    }
    
    public String searchDept()throws Exception{
        
         DepartmentDAO dao = DAOFactory.createDepartmentDao();
         
         setListDept(dao.searchDepartment(searchStr));
        
         session.put("deptlist", listDept);
        
        return SUCCESS;
    }
    
    public String addDept() throws Exception {
        try {
                    DepartmentDAO dao = DAOFactory.createDepartmentDao();

               int i = dao.insertSingle(newDept);
               setDeptID(newDept.getDepartmentID());

               if (i == 1) {
                   setMessage("เพิ่มข้อมูลเรียบร้อยแล้ว");
                  return "success"; 
               }else{
                  setMessage("ไม่สามารถเพิ่มข้อมูลได้");
                  return ERROR; 
               }
        } catch (Exception e) {
              setMessage(e.getMessage());
              return ERROR; 
        }
       
        
    }
    
    
    public String updateDept() throws Exception {
        
        System.out.println(newDept.getDepartmentName());
        
       
      // newDept.setDepartmentName(new String(newDept.getDepartmentName().getBytes(), "ISO-8859-1"));
        
        DepartmentDAO dao = DAOFactory.createDepartmentDao();
        
        int i = dao.updateSingle(newDept,dept.getDepartmentID());
        
        if (i == 1) {
             setMessage("อัพเดทข้อมูลเรียบร้อยแล้ว");
           return "success"; 
        }else{
           return ERROR; 
        }
        
        
    }
    
    
    public String getDeptByID() {
        try {
            DepartmentDAO dao = DAOFactory.createDepartmentDao();
            
            setDept(dao.getDeptByID(deptID));
            
            return "success";
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(DepartmentAction.class.getName()).log(Level.SEVERE, null, ex);
            return ERROR;
        }
    }
    
    
    
    public static Logger getLOG() {
        return LOG;
    }

    public static void setLOG(Logger LOG) {
        ActionSupport.LOG = LOG;
    }

    public Map<String, Object> getSession() {
        return session;
    }
    
      
    
    @Override
    public void setSession(Map<String, Object> map) {
        this.session = map;
    }

    public String getDeptID() {
        return deptID;
    }

    public void setDeptID(String deptID) {
        this.deptID = deptID;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }

    public Department getNewDept() {
        return newDept;
    }

    public void setNewDept(Department newDept) {
        this.newDept = newDept;
    }

    public List<Department> getListDept() {
        return listDept;
    }

    public void setListDept(List<Department> listDept) {
        this.listDept = listDept;
    }

    public String getSearchStr() {
        return searchStr;
    }

    public void setSearchStr(String searchStr) {
        this.searchStr = searchStr;
    }
    
    
    
}
